package geppetto.phraseScorer;

import geppetto.cat.corpus.BilingualCorpus;

public class PurposeBasedScorer extends AbstractPhraseScorer{

	String _type;
	BilingualCorpus _corpus;
	double rightScore = 1;
	double wrongScore = 0.05;
	
	
	public PurposeBasedScorer(String type, BilingualCorpus corpus) {
		super();
		this._type = type;
		this._corpus = corpus;
	}

	@Override
	public double getPhraseScore(int sentenceNumber, byte sentenceSource, int[] foreingSentence, int[] sourceSentence, int sourceStartPos, int sourceEndPos, int foreignStartPos, int foreignEndPos) {
		if(rightType(sentenceNumber, sourceStartPos, sourceEndPos, foreignStartPos, foreignEndPos)){
			return rightScore;
		}
		return wrongScore;
	}
	
	private boolean rightType(int sentenceNumber, int startS,
			int endS, int startT, int endT) {
		if(_type.toLowerCase().equals("affirmative") || _type.toLowerCase().equals("a")){
			return detectAffirmative(sentenceNumber, startS, endS, startT, endT);
		}
		if(_type.toLowerCase().equals("interrogative") || _type.toLowerCase().equals("i")){
			return detectInterrogative(sentenceNumber, startS, endS, startT, endT);
		}
		if(_type.toLowerCase().equals("exclamative") || _type.toLowerCase().equals("e")){
			return detectExclamative(sentenceNumber, startS, endS, startT, endT);
		}
		if(_type.toLowerCase().equals("declarative") || _type.toLowerCase().equals("d")){
			return detectDeclarative(sentenceNumber, startS, endS, startT, endT);
		}
		return false;
	}

	private boolean detectAffirmative(int sentenceNumber, int startS,
			int endS, int startT, int endT){
		return findIfNextPunctuationMatches(sentenceNumber, startS, endS, startT, endT, ".");
	}

	private boolean detectInterrogative(int sentenceNumber, int startS,
			int endS, int startT, int endT){
		return findIfNextPunctuationMatches(sentenceNumber, startS, endS, startT, endT, "?");
	}
	private boolean detectExclamative(int sentenceNumber, int startS,
			int endS, int startT, int endT){
		return findIfNextPunctuationMatches(sentenceNumber, startS, endS, startT, endT, "!");
	}
	private boolean detectDeclarative(int sentenceNumber, int startS,
			int endS, int startT, int endT){
		return findIfNextPunctuationMatches(sentenceNumber, startS, endS, startT, endT, ".");
	}

	private boolean findIfNextPunctuationMatches(int sentenceNumber, int startS,
			int endS, int startT, int endT, String match){
		boolean sourceIsCorrect = false;
		boolean targetIsCorrect = true;//false;

		int[] source = _corpus.getSourceSentence(sentenceNumber, BilingualCorpus.TRAIN_CORPUS);
		int[] target = _corpus.getForeignSentence(sentenceNumber, BilingualCorpus.TRAIN_CORPUS);
		for(int i = endS; i > 0 && i < source.length; i++){
			String sourceC = _corpus.getSourceString(source[i]);
			if (isPunct(sourceC)) {
				if(sourceC.equals(match)){
					sourceIsCorrect = true;
				}
				break;
			}
		}
		/*for(int j = endT; j < target.length; j++){
			String targetC = _corpus.getForeignString(target[j]);
			if (isPunct(targetC)) {
				if(targetC.equals(match)){
					targetIsCorrect = true;
				}
				break;
			} 
		}*/
		return sourceIsCorrect && targetIsCorrect;
	}

	public boolean isPunct(String s) {
		if (s.equalsIgnoreCase(".")) {
			return true;
		} else if (s.equalsIgnoreCase("!")) {
			return true;
		} else if (s.equalsIgnoreCase("?")) {
			return true;
		} else {
			return false;
		}
	}
}
